import numpy as np
import random as rd

from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QMainWindow, QDialog

from firstView import Ui_Window
from function import RandomizedQuickSort, BinarySearch
from secondView import Ui_childWindow


class MyWindow(QMainWindow, Ui_Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setupUi(self)
        self.sortedArray = None
        self.input1.setPlaceholderText("请输入由英文逗号隔开的实数序列")
        self.output1.setPlaceholderText("请在输入数组完成后点击排序")
        self.input2.setPlaceholderText("请输入一个实数")
        self.output2.setPlaceholderText("请在输入数组并排序，及输入查询元素后再点击查询")
        # 设置输入限制
        self.setWindowTitle("快排与二分搜索")
        self.regx1 = QtCore.QRegExp("(([+-]?[0-9]+(\.[0-9]+)?)\,)*([+-]?[0-9]+(\.[0-9]+)?)$")
        validator1 = QtGui.QRegExpValidator(self.input1)
        validator1.setRegExp(self.regx1)
        self.input1.setValidator(validator1)
        self.run_sort.clicked.connect(self.runSort)

        self.regx2 = QtCore.QRegExp("([+-]?[0-9]+(\.[0-9]+)?)$")
        validator2 = QtGui.QRegExpValidator(self.input2)
        validator2.setRegExp(self.regx2)
        self.input2.setValidator(validator2)
        self.run_search.clicked.connect(self.runSearch)

    def runSort(self):
        inputlist = self.input1.text()
        if inputlist == '':
            self.sortedArray = None
            self.output1.setText("请先输入")
            return
        b = inputlist.split(',')
        self.sortedArray = [float(x) for x in b if x is not '']
        RandomizedQuickSort(self.sortedArray, 0, len(self.sortedArray) - 1)
        self.output1.setText(str(self.sortedArray))

    def runSearch(self):
        if self.sortedArray is None or []:
            self.output2.setText("请先排序")
            return
        target = self.input2.text()
        if target is '':
            self.output2.setText("请先输入")
            return
        else:
            target = float(target)
        ans = BinarySearch(self.sortedArray, target, len(self.sortedArray))
        if ans == -1:
            self.output2.setText("未查询到")
        else:
            self.output2.setText("所查目标位置:" + str(ans))


class ChildWindow(QDialog, Ui_childWindow):
    def __init__(self):
        super(ChildWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("随机测试数据")
        self.test_data()
        self.pushButton.clicked.connect(self.runButton)

    def runButton(self):
        self.test_data()

    def test_data(self):
        n = 10
        result_str = "开始测试\n"
        while n > 0:
            n -= 1
            tmp_size = rd.randint(2, 100)
            a = np.empty([tmp_size], dtype=int)
            result_str += "随机数组长度：" + str(tmp_size) + "\n排序前：" + str(a)
            RandomizedQuickSort(a, 0, len(a) - 1)
            result_str += "\n排序后：" + str(a)
            target = rd.choice(list(a) + list(range(0, 99)))
            result_str += "\n随机查询：" + str(target) + "所在位置：a[" + str(BinarySearch(a, target, len(a))) + "]\n"
        self.textEdit.setText(result_str)
